package com.telit.common.util;


import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2022/6/29 7:47
 */

public class TiandituUtlis {

    private static Logger logger = LoggerFactory.getLogger(TiandituUtlis.class);

    private static final String GEOCODER_URL = "http://api.tianditu.gov.cn/geocoder";

    private static final String TYPE="geocode";

    private static final String TK="4bb4ac93ef3dbc13b4605de1e0734ae6";

    public static String getDetailAddress(String lon, String lat) {
        try{
            Map<String, String> params = new HashMap<>();
            params.put("postStr","{'lon':"+lon+",'lat':"+lat+",'ver':1}");
            params.put("type","geocode");
            params.put("tk","4bb4ac93ef3dbc13b4605de1e0734ae6");
            String result = HttpClientUtil.doGet(GEOCODER_URL, params, null);
            JSONObject rcvBodyJson = JSONObject.parseObject(result);
            String address=rcvBodyJson.getJSONObject("result").getString("formatted_address");
            return address;
        }catch (Exception e){
            logger.error("通过天地图接口获取地址失败e:{}",e);
            return null;
        }

    }

    public static void main(String[] args){
        System.out.println(TiandituUtlis.getDetailAddress("119.13669473026566","31.190847222226644"));
    }

}
